//
//  SINAPersonInformationViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/28.
//  Copyright © 2017年  androidlongs. All rights reserved.
//  个人资料页面

#import "SINATecPersonInformationViewController.h"
#import "SINAUserTitleDetaileViewController.h"
#import "SINAStuApproveUserNameViewController.h"
#import "SINALoginViewController.h"
#import "SINANavLoginViewController.h"
#import "SINAControllerUtils.h"
#import "SINAStuMyAlertSignNameController.h"

@interface SINATecPersonInformationViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *mainScroolView;

//我的头像
@property (weak, nonatomic) IBOutlet UIImageView *myTitleImageView;
//个性签名
@property (weak, nonatomic) IBOutlet UILabel *signFlagLabel;
//用户姓名
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
//用户性别
@property (weak, nonatomic) IBOutlet UILabel *userSexLabel;
//大学
@property (weak, nonatomic) IBOutlet UILabel *schoolLabel;
//职称
@property (weak, nonatomic) IBOutlet UILabel *schoolClassLabel;
//院系
@property (weak, nonatomic) IBOutlet UILabel *schoolSubLabel;
@end

@implementation SINATecPersonInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"我的资料";
    self.mainScroolView.showsVerticalScrollIndicator=NO;
    self.mainScroolView.showsHorizontalScrollIndicator=NO;
   
    WeakSelf;
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.signFlagLabel andWithBlick:^(UIView *view) {
        SINAStuMyAlertSignNameController *alertSignController = [[SINAStuMyAlertSignNameController alloc]initWithNibName:@"SINAStuMyAlertSignNameController" bundle:nil];
        [weakSelf.navigationController pushViewController:alertSignController animated:YES];
    }];
}
//修改头像页面
- (IBAction)alertTitleClickFunction:(UIButton *)sender {
    SINAUserTitleDetaileViewController
    *settingController = [[SINAUserTitleDetaileViewController alloc]initWithNibName:@"SINAUserTitleDetaileViewController" bundle:nil];
    settingController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:settingController animated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
    [super viewWillAppear:animated];
    
    
    SINAUserModel *userModel = [[SINACommonUserHelper share] getUserModel];
    if (userModel!=nil) {
        //设置昵称
        NSString *userNikName = userModel.userRealName;
        if (userNikName==nil||[userNikName isEqualToString:@""]) {
            NSString *phone = userModel.userPhone;
            if (phone==nil||[phone isEqualToString:@""]) {
                phone=@"去认证";
            }else{
                phone=@"去认证";
            }
            userNikName = phone;
        }
        self.userNameLabel.text=userNikName;
        //个性签名
        NSString *sign =userModel.sign;
        if (sign==nil||sign.length==0) {
            sign=@"设置签名";
        }
        self.signFlagLabel.text = sign;
        
        //头像
        NSString *headerImageUrl = userModel.userTitleImageUrl;
        if (headerImageUrl!=nil&&headerImageUrl.length>1) {
            NSURL *imageUrl =[NSURL URLWithString:headerImageUrl];
            [self.myTitleImageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"默认头像"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                DLog(@"加载头像完成");
                if (error!=nil) {
                    DLog(@"加载头像失败 ");
                }
            }];
        }
        
        //性别
        NSString *userSex = userModel.userSex;
        if (userSex==nil||userSex.length==0) {
            self.userSexLabel.text=@"未知";
        } else {
            if ([userSex integerValue]==1) {
                self.userSexLabel.text=@"男";
            } else {
                self.userSexLabel.text=@"女";
            }
        }
        if (userModel.universityid!=nil&&userModel.universityid.length>0) {
            //有大学信息
            [self loadingUserInformationFunction:userModel];
        } else {
            //无大学信息
            NSString *universityname=@"去实名认证";
            NSString *realname=@"去实名认证";
            NSString *collname=@"去实名认证";
            NSString *classifyname=@"去实名认证";
            
            self.userNameLabel.text=realname;
            self.schoolLabel.text=universityname;
            self.schoolSubLabel.text=collname;
            self.schoolClassLabel.text=classifyname;
            WeakSelf;
            [[SINAUiviewClickLiserner alloc]viewClickFunction:self.userNameLabel andWithBlick:^(UIView *view) {
                if ([weakSelf checkApproveIdent]) {
                    [weakSelf toApprove];
                }
            }];
            [[SINAUiviewClickLiserner alloc]viewClickFunction:self.schoolLabel andWithBlick:^(UIView *view) {
                if ([weakSelf checkApproveIdent]) {
                    [weakSelf toApprove];
                }
            }];
            [[SINAUiviewClickLiserner alloc]viewClickFunction:self.schoolSubLabel andWithBlick:^(UIView *view) {
                if ([weakSelf checkApproveIdent]) {
                    [weakSelf toApprove];
                }
            }];
            [[SINAUiviewClickLiserner alloc]viewClickFunction:self.schoolClassLabel andWithBlick:^(UIView *view) {
                if ([weakSelf checkApproveIdent]) {
                    [weakSelf toApprove];
                }
            }];
        }
        
    }else{
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"用户信息异常，请重新登录" sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
    }
}

//获取用户个人信息详情
-(void) loadingUserInformationFunction:(SINAUserModel*)userModel{
    NSString *userUid=userModel.userUid;
    DLog(@"获取用户个人信息详情 uid is %@",userUid);
    if (userUid==nil||userUid.length<1) {
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"用户信息异常，请重新登录" sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
    } else {

        [self startLoading];
        WeakSelf;
        [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@?uuid=%@",baseUrl,userInformationUrl,userUid]  success:^(id responseData) {
            NSString *code = responseData[@"code"];
            NSString *universityname=@"无";
            NSString *realname=@"无";
            NSString *collname=@"无";
            NSString *classifyname=@"无";
            [weakSelf stopLoading];
            if ([code isEqualToString:@"1000"]) {
                NSDictionary *personInfromation = responseData[@"content"];
                universityname=personInfromation[@"universityname"];
                realname=personInfromation[@"realname"];
                collname=personInfromation[@"collname"];
                classifyname=personInfromation[@"classifyname"];
                
            }else if ([code isEqualToString:@"1003"]) {
                WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"验证失败" sureBtn:@"确定" cancleBtn:nil];
                [alertView showXLAlertView];
                
                universityname=@"加载失败";
                realname=@"加载失败";
                collname=@"加载失败";
                classifyname=@"加载失败";
            }else{
                NSString *message =responseData[@"message"];
                WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定" cancleBtn:nil];
                [alertView showXLAlertView];
                universityname=@"加载失败";
                realname=@"加载失败";
                collname=@"加载失败";
                classifyname=@"加载失败";
            }
            
            weakSelf.userNameLabel.text=realname;
            weakSelf.schoolLabel.text=universityname;
            weakSelf.schoolSubLabel.text=collname;
            weakSelf.schoolClassLabel.text=classifyname;
        } failure:^(NSError *error, NSInteger errCode) {
            [weakSelf stopLoading];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"网络异常 稍后重试" sureBtn:@"确定" cancleBtn:nil];
            [alertView showXLAlertView];
            
            NSString *universityname=@"无";
            NSString *realname=@"无";
            NSString *collname=@"无";
            NSString *classifyname=@"无";
            
            universityname=@"加载失败";
            realname=@"加载失败";
            collname=@"加载失败";
            classifyname=@"加载失败";
            
            weakSelf.userNameLabel.text=realname;
            weakSelf.schoolLabel.text=universityname;
            weakSelf.schoolSubLabel.text=collname;
            weakSelf.schoolClassLabel.text=classifyname;
        }];
    }
}

//校验身份验证
-(BOOL) checkApproveIdent{
//    WeakSelf;
    SINAUserModel *userModel = [[SINACommonUserHelper share]getUserModel];
    if (userModel==nil) {
        //重新登录
        SINALoginViewController *rootViewControll = [[SINALoginViewController alloc]init ];
        SINANavLoginViewController *homeController = [[SINANavLoginViewController alloc] initWithRootViewController:rootViewControll];
        [[SINAControllerUtils alloc]restoreRootViewController:homeController andWithWindow:self.view.window];
        return NO;
    } else {
        //校验
        NSString *universityid = userModel.universityid;
        if (universityid==nil||universityid.length==0) {
            return YES;
        }
        return NO;
    }
}
//去认证
-(void) toApprove{
    SINAStuApproveUserNameViewController *stuApproveUserNameController = [[SINAStuApproveUserNameViewController alloc]initWithNibName:@"SINAStuApproveUserNameViewController" bundle:nil];
    stuApproveUserNameController.openType=1;
    stuApproveUserNameController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:stuApproveUserNameController animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
